package com.example.cose;

import com.example.util.AesUtil;
import com.example.util.JsonUtil;
import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * Created by zsl on 2017/9/3.
 */
@Configuration
public class ConfigurationFilter {
    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }

    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());//添加过滤器
        registration.addUrlPatterns("/*");//设置过滤路径，/*所有路径
        registration.addInitParameter("name", "alue");//添加默认参数
        registration.setName("MyFilter");//设置优先级
        registration.setOrder(1);//设置优先级
        return registration;
    }

    public class MyFilter implements Filter {
        @Override
        public void destroy() {
        }

        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain
                chain)
                throws IOException, ServletException {

            HttpServletRequest hreq = (HttpServletRequest) req;
            String uri = hreq.getRequestURI();
            if(uri.contains("ureport")
                    || uri.contains("upload")){
                // 报表的请求，不做处理  图像上传的请求，不做处理
                chain.doFilter(req, res);
            } else{
                String reqMethod = hreq.getMethod();
                //如果是POST请求则需要获取 param 参数
                if("POST".equals(reqMethod)){

                    HttpServletResponse response = (HttpServletResponse) res;
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("application/json; charset=utf-8");

                    BodyReaderHttpServletRequestWrapper requestWrapper = new BodyReaderHttpServletRequestWrapper(hreq);
                    String body = HttpHelper.getBodyString(requestWrapper);

//                    String param = URLDecoder.decode(body,"utf-8");

                    //解密
                    String aeStr = "";
                    if (!StringUtils.isEmpty(body) && body.contains("securityParam")) {
                        body = JsonUtil.jsonToPojo(body, Map.class).get("securityParam")+"";
                        try {
                            body = AesUtil.aesDecrypt(body);
                            //数据重新写回
                            body = new String(Base64Utils.decodeFromString(body),"UTF-8");
                            requestWrapper.setBody(body);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }


                    chain.doFilter(requestWrapper, res);

                }else{
                    //get请求直接放行
                    chain.doFilter(req, res);
                }
            }
        }

        @Override
        public void init(FilterConfig arg0) throws ServletException {
        }
    }
}